<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-12-24
 * Time: 8:56
 */

// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\technician\controller;

/** 控制器类引入区 **/

use app\common\controller\Alipay;
use app\common\controller\Smscm;
use app\common\controller\Core;

/**模型类引入区 **/

use app\common\controller\Wxpay;
use app\common\model\Order as OrdersM;
/** 核心或第三方类引入区 **/

use app\common\model\Users;
use app\common\model\ViperCard;
use app\common\model\ViperUser;
use app\common\model\VipRechargeLog;
use Carbon\Carbon;
use my\Wx;
use think\Db;
use think\Validate;

/**
 * 前台首页控制器
 * @package app\api\controller
 */
class Card extends Home
{
    public $noNeedLogin = ['check_is_pay'];

    /**
     *  会员卡查询
     **/
    public function vip_serch()
    {
        if ($this->request->isPost()) {
            $phone= $this->request->post('phone');
            $VipcardModel= new ViperCard();
            $list=$VipcardModel->serch_phone($phone,$this->store_id);
            $this->result('获取成功', $list, 200);
        } else {
            $this->result('非法请求', '', 204);
        }
    }




    /**
     *  会员卡详情
     **/
    public function card_info()
    {
        if ($this->request->isPost()) {
            $id= $this->request->post('id');
            $VipcardModel= new ViperCard();
            $list=$VipcardModel->card_info($id,$this->store_id);
            $this->result('获取成功', $list, 200);
        } else {
            $this->result('非法请求', '', 204);
        }
    }

    /**
     * 充值视图
     */
    public function  get_rechare_tem(){
        if ($this->request->isPost()) {
            $vip_type_id = $this->request->post('viper_type_id');
            $VipcardModel = new ViperCard();
            $list=$VipcardModel->get_recharge_tem($this->store_id,$vip_type_id);
            $list = array_values($list);
            $info=array(
                'list'=>$list,
            );
            $this->result('获取成功', $info, 200);
        } else {
            $this->result('非法请求', '', 204);
        }
    }
    /**
     * 生成支付二维码
     */
    public function  recharge_do(){
        if ($this->request->isPost()) {
            $id= $this->request->post('id');
            $user_id=$this->userInfo['id'];
            $recharge_tempid=$this->request->post('recharge_tempid');
            $pay_type=$this->request->post('pay_type');
            $roomId=$this->request->post('room_id');
            $money=$this->request->post('room_id');
            $VipcardModel= new ViperCard();
            if($recharge_tempid == '0'){
                $list=$VipcardModel->recharge_do($this->store_id,$id,$pay_type,$recharge_tempid,$roomId,$user_id,$money);
            }else{
                $list=$VipcardModel->recharge_do($this->store_id,$id,$pay_type,$recharge_tempid,$roomId,$user_id);
            }
            $this->result('下单成功', $list, 200);

        } else {
            $this->result('非法请求', '', 204);
        }
    }
    /**
     * 查询订单是否已经支付
     */
    public  function  check_is_pay(){
        $id=$this->request->post('id');
        $re=VipRechargeLog::where('id',$id)->find();
        if($re['pay_status']=='1'){
            $this->result( '', '', 200);
        }else{
            $this->result( '', '', 204);
        }
    }

    /**
     *获取开卡模板
     */
    public function  get_open_tem(){
        if ($this->request->isPost()) {
            $VipcardModel= new ViperCard();
            $list=$VipcardModel->get_cardtype($this->store_id);
            $info=array(
                'list'=>$list,
            );
            $this->result('获取成功', $info, 200);
        } else {
            $this->result('非法请求', '', 204);
        }
    }
    /**
     *  会员开卡
     **/
    public function open_card()
    {
        if ($this->request->isPost()) {
            $vip_type_id=$this->request->post('viper_type_id');
            $template_id=$this->request->post('template_id');
            $passwd=$this->request->post('passwd');
            $birthday=$this->request->post('birthday');
            $pay_type=$this->request->post('pay_type');
            $mobile=$this->request->post('phone');
            $roomId=$this->request->post('room_id');
            $money=$this->request->post('money');
            $VipcardModel= new ViperCard();
            $list=$VipcardModel->open_Card($this->store_id,$vip_type_id,$template_id,$mobile,$this->userInfo['id'],$passwd,$birthday,$pay_type,$roomId,$money);
            $this->result('获取成功', $list, 200);
        } else {
            $this->result('非法请求', '', 204);
        }

    }

    /**
     * 计算会员价格
     */
    public function vip_discount()
    {
        if ($this->request->isPost()) {
            $getData = $this->request->post();
            if (empty($getData['viper_type_id'])) {
                $this->result('缺少参数', '', 204);
            }
            //项目
            if (!empty($getData['project'])) {
                $projectRes = json_decode($getData['project'], true);
                foreach ($projectRes as $key => $value) {
                    //查询项目是否有活动价
                    $dis = Db::name('qt_viper_activity_project')
                        ->where(['project_id' => $value['project_id'], 'viper_type_id' => $getData['viper_type_id']])
                        ->find();
                    if (!empty($dis)) {
                        //判断是否有会员价
                        if($dis['type'] != null){
                            $projectRes[$key]['activity_price'] = $dis['viper_price'];
                            if($dis['rule'] == '2'){
                                $time = time();
                            }else{
                                $time  = Db::name('qt_order_project')->where(['id'=>$value['project_order_id']])->value('created_time');
                            }
                            //存在时段会员价
                            if($dis['type'] == '1'){
                                //每天
                                $startTime = strtotime(date('Y-m-d').' '.$dis['start']);
                                $endTime = strtotime(date('Y-m-d').' '.$dis['end']);
                                if($startTime < $time && $time < $endTime) {
                                    if($value['order_type'] == '1'){
                                        $projectRes[$key]['activity_price'] = $dis['range_viper_price'];
                                    }elseif($value['order_type'] == '5'){
                                        $projectRes[$key]['activity_price'] = $dis['range_add_block_price'];
                                    }
                                }
                            }
                            if($dis['type'] == '2'){
                                //每周星期几
                                $value = explode(',',$dis['value']);
                                $week  = Carbon::now()->dayOfWeek;
                                if(in_array($week,$value)){
                                    $startTime = strtotime(date('Y-m-d').' '.$dis['start']);
                                    $endTime = strtotime(date('Y-m-d').' '.$dis['end']);
                                    if($startTime < $time && $time < $endTime){
                                        if($value['order_type'] == '1'){
                                            $projectRes[$key]['activity_price'] = $dis['range_viper_price'];
                                        }elseif($value['order_type'] == '5'){
                                            $projectRes[$key]['activity_price'] = $dis['range_add_block_price'];
                                        }
                                    }
                                }

                            }
                            if($dis['type'] == '3'){
                                //每月几天
                                $value = explode(',',$dis['value']);
                                $day = date('d');
                                if(in_array($day,$value)){
                                    $startTime = strtotime(date('Y-m-d').' '.$dis['start']);
                                    $endTime = strtotime(date('Y-m-d').' '.$dis['end']);
                                    if($startTime < $time && $time < $endTime){
                                        if($value['order_type'] == '1'){
                                            $projectRes[$key]['activity_price'] = $dis['range_viper_price'];
                                        }elseif($value['order_type'] == '5'){
                                            $projectRes[$key]['activity_price'] = $dis['range_add_block_price'];
                                        }
                                    }
                                }
                            }
                        }else{
                            $projectRes[$key]['activity_price'] = $dis['viper_price'];
                            if($value['order_type'] == '5'){
                                $projectRes[$key]['activity_price'] = $dis['add_block_price'];
                            }
                        }
                    } else {
                        $projectRes[$key]['activity_price'] = $value['price'];
                        if($value['order_type'] == '2'){
                            $projectRes[$key]['activity_price'] = $dis['add_block_price'];
                        }
                    }
                }
            } else {
                $projectRes = [];
            }
            //商品
            if (!empty($getData['goods'])) {
                $goodsRes = json_decode($getData['goods'], true);
                foreach ($goodsRes as $key => $value) {
                    //查询项目是否有活动价
                    $dis = Db::name('qt_viper_activity_goods')
                        ->where(['product_id' => $value['goods_id'], 'viper_type_id' => $getData['viper_type_id']])
                        ->find();
                    if (!empty($dis)) {
                        //判断是否有会员价
                        if($dis['type'] != null){
                            $goodsRes[$key]['activity_price'] = $dis['viper_price'];
                            if($dis['rule'] == '2'){
                                $time = time();
                            }else{
                                $time  = Db::name('qt_product_order_item')->where(['id'=>$value['product_order_id']])->value('create_time');
                            }
                            //存在时段会员价
                            if($dis['type'] == '1'){
                                //每天
                                $startTime = strtotime(date('Y-m-d').' '.$dis['start']);
                                $endTime = strtotime(date('Y-m-d').' '.$dis['end']);
                                if($startTime < $time && $time < $endTime){
                                    $goodsRes[$key]['activity_price'] = $dis['rang_viper_price'];
                                }
                            }
                            if($dis['type'] == '2'){
                                //每周星期几
                                $value = explode(',',$dis['value']);
                                $week  = Carbon::now()->dayOfWeek;
                                if(in_array($week,$value)){
                                    $startTime = strtotime(date('Y-m-d').' '.$dis['start']);
                                    $endTime = strtotime(date('Y-m-d').' '.$dis['end']);
                                    if($startTime < $time && $time< $endTime){
                                        $goodsRes[$key]['activity_price'] = $dis['rang_viper_price'];
                                    }
                                }

                            }
                            if($dis['type'] == '3'){
                                //每月几天
                                $value = explode(',',$dis['value']);
                                $day = date('d');
                                if(in_array($day,$value)){
                                    $startTime = strtotime(date('Y-m-d').' '.$dis['start']);
                                    $endTime = strtotime(date('Y-m-d').' '.$dis['end']);
                                    if($startTime < $time && $time < $endTime){
                                        $goodsRes[$key]['activity_price'] = $dis['rang_viper_price'];
                                    }
                                }
                            }
                        }else{
                            $goodsRes[$key]['activity_price'] = $dis['viper_price'];
                        }
                    } else {
                        $goodsRes[$key]['activity_price'] = $value['goods_price'];
                    }
                }
            } else {
                $goodsRes = [];
            }
            $result['project'] = $projectRes;
            $result['goods'] = $goodsRes;
            $this->result('查询成功', $result, 200);
        }
    }

    /**
     * 计算折扣
     */
    public function discount(){
        if($this->request->isPost()){
            $getData = $this->request->post();
            if(empty($getData['active_id'])){
                $this->result('缺少参数','',204);
            }
            //项目
            if(!empty($getData['project'])){
                $projectRes = json_decode($getData['project'],true);
                foreach ($projectRes as $key=>$value){
                    //查询项目是否有活动价
                    $dis = Db::name('qt_data_activity_item')
                        ->where(['item_type'=>'project','item_id'=>$value['project_id'],'activity_id'=>$getData['active_id']])
                        ->find();
                    if(!empty($dis)){
                        if(isset($getData['viper_card_type_id']) && !empty($getData['viper_card_type_id'])){
                            $vip = Db::name('qt_data_activity_item_viper')->where(['activity_item_id'=>$dis['id'],'viper_card_type_id'=>$getData['viper_card_type_id']])->find();
                        }else{
                            $vip = Db::name('qt_data_activity_item_viper')->where(['activity_item_id'=>$dis['id']])->find();
                        }
                        if(empty($vip)){
                            $viper_price =  $value['price'];
                            $viper_add_block_price =  '';
                        }else{
                            $viper_price =  $vip['viper_price'];
                            $viper_add_block_price = $vip['viper_add_block_price'];
                        }
                        $projectRes[$key]['activity_price'] = $dis['activity_price'];
                        $projectRes[$key]['add_block_price'] = $dis['add_block_price'];
                        $projectRes[$key]['viper_price'] = $viper_price;
                        $projectRes[$key]['viper_add_block_price'] = $viper_add_block_price;
                    }else{
                        $projectRes[$key]['activity_price'] = $value['price'];
                        $projectRes[$key]['add_block_price'] = '';
                        $projectRes[$key]['viper_price'] = $value['price'];
                        $projectRes[$key]['viper_add_block_price'] = '';
                    }

                }
            }else{
                $projectRes = [];
            }
            //商品
            if(!empty($getData['goods'])){
                $goodsRes = json_decode($getData['goods'],true);
                foreach ($goodsRes as $key=>$value){
                    //查询项目是否有活动价
                    $dis = Db::name('qt_data_activity_item')
                        ->where(['item_type'=>'goods','item_id'=>$value['goods_id'],'activity_id'=>$getData['active_id']])
                        ->find();
                    if(!empty($dis)){
                        $goodsRes[$key]['activity_price'] = $dis['activity_price'];
                        $goodsRes[$key]['add_block_price'] = $dis['add_block_price'];

                    }else{
                        $goodsRes[$key]['activity_price'] = $value['goods_price'];
                        $goodsRes[$key]['add_block_price'] = '';
                    }
                }
            }else{
                $goodsRes = [];
            }
            $result['project'] = $projectRes;
            $result['goods'] = $goodsRes;
            $this->result('查询成功',$result,200);
        }
    }
}
